package com.sram.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.sram.VO.PageVO;
import com.sram.entity.Function;
import com.sram.entity.Photo;
import com.sram.entity.User;
import com.sram.util.JSONUtils;

public class UserAction extends BaseAction{
	Map<String, Object> jsonMap = new HashMap<String, Object>();
	private User u;
	public User getU() {
		return u;
	}
	public void setU(User u) {
		this.u = u;
	}
	public String regist() throws Exception{
		System.out.println("denglu"+u);
		if(validationShopInfoNew()){
		if(u!=null){
			jsonMap.put("saveShopNewSUCCESS", true);
			userService.saveUser(u);
		}else{
			jsonMap.put("saveShopNewSUCCESS", false);

		}
		}
		System.out.println("seshisvn");
		return JSONUtils.jsonOut(jsonMap);
	}
	//后台验证注册
	private boolean validationShopInfoNew() {
		boolean flagnew = true;
		String name=u.getUserName();
		String pwd=u.getPassword();
		String pwReg = "^[A-Za-z0-9]+$";
		Pattern pattern = Pattern.compile(pwReg);//����������ʽ  
		Matcher matcher = pattern.matcher(name);
		if(!matcher.matches()){
			flagnew = false;
			return flagnew;
		}else if(name.length() < 6){
			flagnew = false;
			return flagnew;
		}else if(name.length() > 18){
			flagnew = false;
			return flagnew;
		}else {
			if(userService.usernameisUnique(name) != null){
				flagnew = false;
				return flagnew;
			}
		}
		return flagnew;
	}
	public String login() throws Exception{		
		String  name=ServletActionContext.getRequest().getParameter("name");
		String  pwd=ServletActionContext.getRequest().getParameter("pwd");
		
		
		User online_user = userService.login(name,pwd);
		if(online_user == null){
			jsonMap.put("isSUCCESS",false);
		}else{
			jsonMap.put("isSUCCESS", true);
			ServletActionContext.getRequest().getSession().setAttribute("username", name);
		}
		return JSONUtils.jsonOut(jsonMap);
	}
	public String user_isUnique() throws Exception{
		String  username=ServletActionContext.getRequest().getParameter("userName");
		u=userService.usernameisUnique(username);
		if(u!=null){
			jsonMap.put("isUniqueSUCCESS", true);
		}else{
			jsonMap.put("isUniqueSUCCESS", false);
		}
		return JSONUtils.jsonOut(jsonMap);
	}
	public String tui() throws Exception{
		ServletActionContext.getRequest().getSession().invalidate();
		return JSONUtils.jsonOut(jsonMap);
	}
	public String find(){
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
		detachedCriteria.add(Restrictions.eq("1", "1"));	
		List<User> user=userService.findUser(detachedCriteria);
		for (User user2 : user) {
			System.out.println(user2.toString());
		}
		return null;
	}
	public String findall(){
		String pageNo1 = this.getRequest().getParameter("pageNo");
		int pageNo = Integer.parseInt((String) (pageNo1 == null || "".equals(pageNo1) ? "1" : pageNo1));
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
		List<User> user=userService.findall();
		//每页显示的数量
		int pageSize = 5;
		//每页第一个的位置
		int firstResult = (pageNo - 1) * pageSize;
        //第几页的集合
		List<User> photos1=userService.findByDetachedCriteriaLimit(detachedCriteria, firstResult, pageSize);
		
		ServletActionContext.getRequest().getSession().setAttribute("indexCheckVos", photos1);
		   PageVO pageVO = new PageVO();
	       pageVO.setPageNo(pageNo);
	       int totals = user.size();
	       Long pages = (long) (totals % pageSize > 0 ? totals / pageSize + 1 : totals / pageSize);		
	       pageVO.setPages(pages);	
	       Long total = (long) user.size();	
	       pageVO.setTotal(total);
	       ServletActionContext.getRequest().getSession().setAttribute("pageVOs", pageVO);
		return "success";
	}
	public String user(){
		if(ServletActionContext.getRequest().getSession().getAttribute("username")!=null){
			System.out.println(ServletActionContext.getRequest().getSession().getAttribute("username"));
			return SUCCESS;
		}else{
			return "false";
		}
		
	}
	public String login_userLoginFunction() throws Exception{
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String name =  (String) ServletActionContext.getRequest().getSession().getAttribute("username");
		if (name != null) {
			System.out.println("1111111111111111111111111");
			List<Function> Functions = functionService.findFunction(name);
			jsonMap.put("Functions", Functions);
		}
			return JSONUtils.jsonOut(jsonMap);
	}
	public String ceshi() throws Exception{
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String name =  (String) ServletActionContext.getRequest().getSession().getAttribute("username");
		if (name != null) {
			System.out.println("1111111111111111111111111");
			List<Function> Functions = functionService.findFunction(name);
			jsonMap.put("Functions", Functions);
		}
			return JSONUtils.jsonOut(jsonMap);
	}
}
